package com.banciyuan.bcywebview.biz.detail.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.util.DisplayMetrics;

import com.banciyuan.bcywebview.utils.file.ByteUtils;
import com.banciyuan.bcywebview.utils.universalimageloader.cache.disc.impl.ext.LruDiscCache;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DetailImageData;

/**
 * Created by squall on 2016/3/9.
 */
public class SplitTask {

    private static final int MAXHEIGHT = 1300;
    private DetailImageData mItem;
    private File mfile;
    private Context context;


    public SplitTask(DetailImageData mItem, File mfile, Context context) {
        this.mItem = mItem;
        this.mfile = mfile;
        this.context = context;
    }

    public List<DetailImageData> split() {
        List<DetailImageData> dlist = new ArrayList<>();
        try {
            byte[] in2b = ByteUtils.parseImageToByte(mfile);
            LruDiscCache lruDiscCache = (LruDiscCache) ImageLoader.getInstance().getDiskCache();
            BitmapRegionDecoder resouse = BitmapRegionDecoder.newInstance(in2b, 0, in2b.length, false);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            int height = resouse.getHeight();

            int width = resouse.getWidth();
            int sWidth = metrics.widthPixels;
            float ratio = (float) sWidth / (float) width;
            int sheight = Math.round(height * ratio);
            if (height > MAXHEIGHT) {
                Bitmap bitmap = null;
                int count = height % MAXHEIGHT == 0 ? height / MAXHEIGHT : height / MAXHEIGHT + 1;
                int end_height = height % MAXHEIGHT == MAXHEIGHT ? MAXHEIGHT : height - ((count - 1) * MAXHEIGHT);
                for (int i = 0; i < count; i++) {
                    String key = mItem.getUrl().substring(1, mItem.getUrl().length()) + i;
                    File splitFile = lruDiscCache.get(key);
                    boolean flag = false;
                    if (splitFile != null) {
                        byte[] result = ByteUtils.parseImageToByte(splitFile);
                        bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
                        if (bitmap != null) {
                            flag = true;
                        }
                    } else {
                        if (i < count - 1) {
                            bitmap = resouse.decodeRegion(new Rect(0, i * MAXHEIGHT, width, (i + 1) * MAXHEIGHT), options);
                            if (bitmap != null) {
                                flag = lruDiscCache.saveDecode(key, bitmap);
                            }
                        } else {
                            bitmap = resouse.decodeRegion(new Rect(0, i * MAXHEIGHT, width, i * MAXHEIGHT + end_height), options);
                            if (bitmap != null) {
                                flag = lruDiscCache.saveDecode(key, bitmap);
                            }
                        }
                    }
                    DetailImageData detailImageData = new DetailImageData(key);
                    if (i < count - 1) {
                        detailImageData.setHeight(Math.round(MAXHEIGHT * ratio));
                    } else {
                        if (end_height < 80) {
                            detailImageData.setHeight(Math.round(end_height * ratio) + 1);
                        } else {
                            detailImageData.setHeight(Math.round(end_height * ratio));
                        }
                    }
                    if (i == 0) {
                        detailImageData.setIs_padding(true);
                    } else {
                        detailImageData.setIs_padding(false);
                    }
                    detailImageData.setIf_size(true);
                    detailImageData.setLocation(mItem.getLocation());
                    detailImageData.setSub_index(mItem.getSub_index());
                    detailImageData.setOrgin_url(mItem.getUrl());
                    dlist.add(detailImageData);
                    if (flag) {
                        detailImageData.onLoadingComplete();
                    } else {
                        detailImageData.onLoadingError();
                    }
                }
            } else {
                mItem.setIf_size(true);
                mItem.setHeight(sheight);
                mItem.setSub_index(mItem.getSub_index());
                mItem.setIs_large(false);
                mItem.onLoadingComplete();
                mItem.setOrgin_url(mItem.getUrl());
                dlist.add(mItem);
            }

        } catch (Exception e) {
            e.printStackTrace();
            mItem.onLoadingError();
            dlist.add(mItem);
        }
        return dlist;
    }
}